//
//  RCDEmptyView.m
//  SchoolHelper
//
//  Created by 任辰东 on 2018/3/29.
//  Copyright © 2018年 JSY. All rights reserved.
//

#import "RCDEmptyView.h"

static const CGFloat kDefaultLineSpaceing = 8;
@interface RCDEmptyView ()

@property(nonatomic, strong) UIView *contentView;
@property(nonatomic, strong) UIImageView *iconImageView;
@property(nonatomic, strong) UILabel *titleLable;
@property(nonatomic, strong) UIButton *nextBtn;
@property(nonatomic, assign) RCDEmptyViewStyle style;
@property(nonatomic, copy) void (^didClickSelf)(void);
@end

@implementation RCDEmptyView

+ (RCDEmptyView *)addEmptyWithStyle:(RCDEmptyViewStyle)style
                            onView:(UIView *)parentView
                          position:(RCDEmptyViewPosition)position
                     clickCallBack:(void (^)(void))clickBlock
{
    [self removeFromParentView:parentView];
    
    RCDEmptyView *emptyView = [[RCDEmptyView alloc] initWithStyle:style];
    if (parentView)
    {
        [parentView addSubview:emptyView];
        
        [emptyView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(parentView);
        }];
        
        [emptyView.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(parentView.mas_centerX);
            if (position == EmptyViewPositionTop) {
                make.centerY.equalTo(parentView.mas_centerY).offset(-CGRectGetHeight(parentView.frame)/5);
            }else if(position == EmptyViewPositionCenter){
                make.centerY.equalTo(parentView.mas_centerY);
            }else{
                make.centerY.equalTo(parentView.mas_centerY).offset(CGRectGetHeight(parentView.frame)/5);
            }
        }];
    }
    [emptyView setDidClickSelf:^{
        if (clickBlock)
        {
            clickBlock();
        }
    }];
    return emptyView;
}

+ (void)removeFromParentView:(UIView *)parentView
{
    //    for (UIView *view in parentView.subviews) {
    //        if ([view isKindOfClass:[XJEmptyView class]]) {
    //            [view removeFromSuperview];
    //        }
    //    }
    [self view:parentView floor:0];
}

+ (void)view:(UIView *)rootView floor:(NSInteger)floor
{
    //    DLog(@"<%@>-第%i层",[rootView class],floor);
    floor++;
    if ([rootView isKindOfClass:[RCDEmptyView class]]) {
        [rootView removeFromSuperview];
    }
    for (UIView *v in [rootView subviews])
    {
        [self view:v floor:floor];
    }
}

- (instancetype)initWithStyle:(RCDEmptyViewStyle)style
{
    self = [super init];
    if (self)
    {
        
        _style = style;
        [self p_drawView];
    }
    return self;
}

- (UIView *)contentView
{
    if (!_contentView)
    {
        _contentView = [UIView new];
        _contentView.userInteractionEnabled = NO;
        _contentView.backgroundColor = [UIColor clearColor];
    }
    return _contentView;
}

- (UIImageView *)iconImageView
{
    if (!_iconImageView)
    {
        _iconImageView = [UIImageView new];
        _iconImageView.userInteractionEnabled = YES;
    }
    return _iconImageView;
}

- (UILabel *)titleLable
{
    if (!_titleLable)
    {
        _titleLable = [UILabel new];
        _titleLable.textAlignment = NSTextAlignmentCenter;
        _titleLable.textColor = UIColorFromHex(0xed5172);
        _titleLable.font = [UIFont systemFontOfSize:13];
        _titleLable.numberOfLines = 2;
    }
    return _titleLable;
}

- (UIButton *)nextBtn{
    if (!_nextBtn) {
        _nextBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        [_nextBtn setTitleColor:UIColorFromHex(0x333333) forState:UIControlStateNormal];
        _nextBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        _nextBtn.backgroundColor = UIColorFromHex(kColorWhite);
        _nextBtn.layer.cornerRadius = 3;
        _nextBtn.clipsToBounds = YES;
        _nextBtn.layer.borderWidth = kOnePixel;
        _nextBtn.layer.borderColor = UIColorFromHex(kColorMain).CGColor;
    }
    return _nextBtn;
}

#pragma mark - draw method

- (void)p_drawView
{
    self.backgroundColor = [UIColor clearColor];
    [self addSubview:self.contentView];
    [self.contentView addSubview:self.iconImageView];
    [self.contentView addSubview:self.titleLable];
    [self.contentView addSubview:self.nextBtn];
    
    [self.nextBtn addTarget:self action:@selector(clickSelf) forControlEvents:UIControlEventTouchUpInside];
    
    [self addTarget:self action:@selector(clickSelf) forControlEvents:UIControlEventTouchUpInside];
    
    WeakSelf
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        StrongSelf
        make.centerX.mas_equalTo(self.mas_centerX);
        make.centerY.mas_equalTo(self.mas_centerY);
        make.left.right.equalTo(self);
    }];
    
    [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        StrongSelf
        make.bottom.equalTo(self.titleLable.mas_top)
        .offset(-33*kScreen_Ratio_W);
        make.centerX.mas_equalTo(self.contentView.mas_centerX);
        make.top.equalTo(self.contentView.mas_top);
    }];
    
    [self.titleLable mas_makeConstraints:^(MASConstraintMaker *make) {
        StrongSelf
        make.centerX.mas_equalTo(self.contentView.mas_centerX);
        make.left.right.equalTo(self.contentView);
    }];
    
    [self.nextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.iconImageView.mas_bottom).offset(15);
        make.width.mas_equalTo(60);
        make.height.mas_equalTo(0);
        make.centerX.equalTo(self.contentView);
        make.bottom.equalTo(self.contentView.mas_bottom);
    }];
    
    [self setContent];
}

- (void)setContent
{
    switch (self.style) {
        case EmptyViewStyleNoLogin:
        {
            [self.nextBtn setTitle:@"登录" forState:UIControlStateNormal];
            [self.nextBtn mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(25);
                make.bottom.equalTo(self.contentView.mas_bottom).offset(-15);
            }];
            [self setImageName:@"userinfo_avatar" titles:@[ @"   "] fontSizes:@[@(15)] colorNames:@[ @(0x8E8E8E) ] lineSpacing:kDefaultLineSpaceing];
        }
            break;
        case EmptyViewStyleCommon:
        {
            [self setImageName:@"data_empty" titles:@[ @"暂无数据"] fontSizes:@[@(15)] colorNames:@[ @(0X8E8E8E) ] lineSpacing:kDefaultLineSpaceing];
        }
            break;
        case EmptyViewStyleClassPatrol:
        {
            [self setImageName:@"data_empty" titles:@[ @"暂无相关信息哦~"] fontSizes:@[@(15)] colorNames:@[ @(0X8E8E8E) ] lineSpacing:kDefaultLineSpaceing];
        }
            break;
        case EmptyViewStyleComment:
        {
            [self setImageName:@"data_empty" titles:@[ @"暂无评论,点击抢沙发"] fontSizes:@[@(15)] colorNames:@[ @(0x8E8E8E) ] lineSpacing:kDefaultLineSpaceing];
        }
            break;
        case EmptyViewStyleNetwork:
        {
            [self setImageName:@"data_empty" titles:@[ @" "] fontSizes:@[@(15)] colorNames:@[ @(0xed5172) ] lineSpacing:kDefaultLineSpaceing];
        }
            break;
        case EmptyViewStyleNoExamination:
        {
            [self setImageName:@"data_empty" titles:@[ @"暂无考试"] fontSizes:@[@(15)] colorNames:@[ @(0x8E8E8E) ] lineSpacing:kDefaultLineSpaceing];
        }
            break;
        case EmptyViewStyleNoAttentions:
        {
            [self setImageName:nil titles:@[ @"暂无关注数据"] fontSizes:@[@(15)] colorNames:@[ @(0xed5172) ] lineSpacing:kDefaultLineSpaceing];
        }
            break;
        case EmptyViewStyleRepair:
        {
            [self setImageName:@"data_empty" titles:@[ @"暂无相关内容"] fontSizes:@[@(15)] colorNames:@[ @(0x8E8E8E) ] lineSpacing:kDefaultLineSpaceing];
        }
            break;
        case EmptyViewStyleDataCenter:
        {
            [self setImageName:@"data_empty" titles:@[ @"暂无数据"] fontSizes:@[@(15)] colorNames:@[ @(0x8E8E8E) ] lineSpacing:kDefaultLineSpaceing];
        }
            break;
        default:
            break;
    }
    
}

- (void)setImageName:(NSString *)imageName title:(NSString *)title
{
    self.iconImageView.image = [UIImage imageNamed:imageName];
    self.titleLable.text = title;
}

- (void)setImageName:(NSString *)imageName titles:(NSArray *)titles fontSizes:(NSArray *)fontSizes colorNames:(NSArray *)colorNames lineSpacing:(CGFloat)lineSpacing
{
    //深度自定义文案显示
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = lineSpacing;
    paragraphStyle.alignment = NSTextAlignmentCenter;
    
    self.iconImageView.image = [UIImage imageNamed:imageName];
    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] init];
    for (int i = 0; i < titles.count; i++)
    {
        NSString *string;
        if (i == titles.count - 1)
        {
            string = titles[i];
        }
        else
        {
            string = [NSString stringWithFormat:@"%@\n", titles[i]];
        }
        NSDictionary *attributes = @{NSFontAttributeName : [UIFont systemFontOfSize:[fontSizes[i] integerValue]],
                                     NSForegroundColorAttributeName : UIColorFromHex([colorNames[i] intValue])};
        [attrString appendAttributedString:[[NSAttributedString alloc] initWithString:string
                                                                           attributes:attributes]];
    }
    
    [attrString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [attrString length])];
    
    self.titleLable.attributedText = attrString;
}

- (void)clickSelf
{
    if (self.didClickSelf)
    {
        self.didClickSelf();
    }
}

@end
